package com.pro.service.imple;

import com.pro.common.GuavaCommon;
import com.pro.mapper.JsDictMapper;
import com.pro.pojo.JsMenu;
import com.pro.service.JsDictService;
import com.pro.utils.GuavaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class JsDictServiceImple implements JsDictService {

    @Autowired
    private JsDictMapper jsDictMapper;

    /**
     *
     * @param type 导航栏类型
     * @param parentId 导航栏父类id
     * @return
     */
    public List<JsMenu> getDictNavData(String type, Integer parentId) {
        return jsDictMapper.getDictNavData(type,parentId);
    }

    /**
     * 整合导航栏数据
     * @param type
     * @return
     */
    public List<JsMenu> getDictNav(String type) {
        List<JsMenu> navData = jsDictMapper.getDictNavData(type, 0);
        for (JsMenu navDatum : navData) {
            navDatum.setChildren(jsDictMapper.getDictNavData(type,navDatum.getMenuId()));
            List<JsMenu> navDatumData = navDatum.getChildren();
            for (JsMenu navDatumDatum : navDatumData) {
                navDatumDatum.setChildren(jsDictMapper.getDictNavData(type,navDatumDatum.getMenuId()));
            }
        }
        return navData;
    }

    /**
     * 获取缓存
     * @param type
     * @return
     */
    public List<JsMenu> getNavCache(String type){
        try {
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.Nav), () -> {
                List<JsMenu> navData = getDictNav(type);
                return navData;
            });
            if (obj instanceof List){
                List<JsMenu> list = (List<JsMenu>)obj;
                return list;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}
